import java.io.*;
import java.util.Properties;

public class FileUtil {
    //序列化方法
    public static void writeObject(String path,Object obj){
        FileOutputStream fos=null;
        ObjectOutputStream oos =null;

        try {
            fos = new FileOutputStream(path);
            oos =new ObjectOutputStream(fos);
            oos.writeObject(obj);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos !=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //反序列化方法
    public static Object rederObject(String path){
        FileInputStream fis =null;
        ObjectInputStream ois =null;
        Object obj=null;

        try {
            fis =new FileInputStream(path);
            ois =new ObjectInputStream(fis);
            return obj= ois.readObject();


        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (fis!= null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    return null;
    }

    //Properties输出
    public static  void soutProperties(String fileName,Properties properties){
        FileOutputStream fos= null;
        try {
            fos=new FileOutputStream(fileName);
            properties.store(fos,null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    public static  Properties inputProperties(String fileName){
        FileInputStream fis =null;
        Properties properties=new Properties();
        try {
            fis =new FileInputStream(fileName);
            properties.load(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return properties;
    }


}
